﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.Resources.ResourceManager
Imports Runa.UI.Effects
Imports Runa.UI.Animation
Imports Runa.UI.Metadata
Imports Runa.Foundation.Diagnostics

Namespace Global.Runa.UI.Controls

    Public Enum SizeMode As Integer
        Auto = 0
        Stretch = 1
        Normal = 2
    End Enum

    Public Class Image
        Inherits ContentControl

        <XamlAwareAttribute(IsPlainType:=False, ElementName:="Foreground", Initializer:=GetType(Texture2DObjectConverter))>
        Public Property Foreground() As Texture2DObject
            Get
                Return Me._Foreground_Actual
            End Get
            Set(value As Texture2DObject)
                Me._Foreground = value
                Me.UpdateSize()
            End Set
        End Property

        Public Property BackgroundClipRect() As Nullable(Of Rectangle)
        Public Property ForegroundClipRect() As Nullable(Of Rectangle)

        <XamlAwareAttribute(IsPlainType:=False, ElementName:="SizeMode", Initializer:=GetType(SizeModeConverter))>
        Public Property SizeMode() As SizeMode
            Get
                Return Me._SizeMode
            End Get
            Set(value As SizeMode)
                Me._SizeMode = value
                Me.UpdateSize()
            End Set
        End Property

        <XamlAwareAttribute(IsPlainType:=False, Initializer:=GetType(Vector2Converter))>
        Public Overrides Property Size As Microsoft.Xna.Framework.Vector2
            Get
                If MyBase.Size = Vector2.Zero Then
                    If Me._Foreground_Actual IsNot Nothing Then
                        If Me._Foreground_Actual.Size <> Vector2.Zero Then
                            MyBase.Size = Me._Foreground_Actual.Size
                        End If
                    End If
                End If
                Return MyBase.Size
            End Get
            Set(value As Microsoft.Xna.Framework.Vector2)
                MyBase.Size = value
            End Set
        End Property

        Private _Foreground As Texture2DObject
        Private _Foreground_Actual As Texture2DObject
        Private _SizeMode As SizeMode

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
            Me._SizeMode = Controls.SizeMode.Normal
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase)
            MyBase.New(Name, Parent)
            Me._SizeMode = Controls.SizeMode.Normal
        End Sub

        Public Overrides Sub Draw(ByVal SpriteBatch As SpriteBatch)
            If (MyBase.Visible AndAlso (Not Me.Foreground Is Nothing)) Then
                If (Not MyBase.Effect Is Nothing) Then
                    SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, Nothing, Nothing, Nothing, MyBase.Effect.Effect)
                Else
                    SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
                End If
                If (Not Me.Foreground.Texture Is Nothing) Then
                    SpriteBatch.Draw(Me.Foreground.Texture, Me.DrawBounds, Me.ForegroundClipRect, Color.White * MyBase.Opacity, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
                End If
                SpriteBatch.End()
            End If
            MyBase.Draw(SpriteBatch)
        End Sub

        Public Sub Trans(ByVal e As TransMaskEffect, ByVal mask As Texture2DObject, ByVal time As TimeSpan)
            'MyBase.Effect = e
            Dim t = MyBase.Background
            MyBase.Background = Me.Foreground
            Me.Foreground = t
            Dim fa As New FloatAnimation() With
            {
                .From = 0.0!,
                .To = 1.0!,
                .Duration = time
            }
            e.Progress = 0.0!
            e.Mask = mask
            e.Old = MyBase.Background
            MyBase.Effect = e
            fa.BeginAnimation(MyBase.Effect, "Progress")
            AddHandler fa.Complete,
                Sub(sender, ea)
                    MyBase.Effect.Animations.Clear()
                    Me.OnAnimationEnd(Me, New EventArgs(Of AnimationBase)(fa))
                End Sub
            Me.OnAnimationBegin(Me, New EventArgs(Of AnimationBase)(fa))
        End Sub

        Private Sub UpdateSize()
            Dim value = Me._Foreground
            If value Is Nothing Then Exit Sub
            Select Case Me.SizeMode
                Case Controls.SizeMode.Stretch
                    If Me.Size <> Vector2.Zero Then
                        Me._Foreground_Actual = New Texture2DObject(value.Name, value.FileName, Me.Game)
                        Me._Foreground_Actual.Texture = value.Scale(Me.Size.X, Me.Size.Y)
                    End If
                Case Controls.SizeMode.Normal
                    Me._Foreground_Actual = value
                Case Controls.SizeMode.Auto
                    Me.Size = value.Texture.GetSize()
                    Me._Foreground_Actual = value
            End Select
        End Sub

        Protected Overrides Sub Dispose(disposing As Boolean)
            'Try
            '    If Me.Background IsNot Nothing Then Me.Background.Dispose()
            '    If Me.Foreground IsNot Nothing Then Me.Foreground.Dispose()
            'Catch ex As Exception
            '    Tracer.Default.TraceMessage(ex.Message & vbCrLf & ex.StackTrace.ToString())
            '    Exit Try
            'End Try
            MyBase.Dispose(disposing)
        End Sub

    End Class

End Namespace